
local jmp_list = {};
local addr_list = {};
local ret_list = {};

function createMake(file)
    --从jmp_list 创建make
    file:write('JMP_SRC += ')
    local newl = '';
    for k,v in ipairs(jmp_list)do
        file:write(newl,v);
        newl = ' \\\n'
    end
    file:write('\n\n'); --换两行结束

    --条件定义的符号
    newl = '';
    file:write('CFLAGS += ')
    for k,v in ipairs(jmp_list)do
        file:write(newl,string.format('-DRET_%08X=0x%X',addr_list[k],ret_list[k]));
        newl = ' \\\n'
    end
    file:write('\n\n'); --换两行结束
end

--源地址，目标地址，设置的寄存器地址，是否是thumb状态
function createJMP(address,dest_address,ret_address,thumb,set_reg)
    local t = thumb;
    assert(address);
    assert(dest_address);
    assert(ret_address);
    set_reg = set_reg or 0;
    if(t)then
        t = ".thumb"
    else
        t = ".arm";
    end
    local fname = string.format('%X.S',address);
    local file = io.open(fname,"wb");
    --local file = io.stdout;
    --如果是arm状态下，可以跳转 +- 32MB

    file:write(string.format([[
    %s
    ldr r%d,=0x%X
    bx  r%d
]],
    t, --thumb
    set_reg,        --目标寄存器
    dest_address,   --目标地址
    set_reg         --跳转的目标寄存器
    ));

    file:close();
    table.insert(jmp_list,fname);
    table.insert(addr_list,address);
    table.insert(ret_list,ret_address);
end
